<script setup name="name">
import { ref, reactive, watchEffect, defineEmits } from 'vue'
import { GetBomAPI } from '@/api/material/common'
const { proxy } = getCurrentInstance()

const props = defineProps({
  bomType: {
    type: String,
    default: ''
  }
})
// 物料类别
// const { sys_material_type } = proxy.useDict('sys_material_type')

const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset'])

const loading = ref(false)

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  materialName: '', // 名称
  materialCode: '', // 编码
  bomType: '' // 类别
})

const handleSelect = ref({ id: '' })
const TableData = ref([])
const total = ref(0)

// 勾选数据
const handleSelectionChange = row => {
  handleSelect.value = row
}

// 是否禁用 保存
const d = computed(() => {
  return handleSelect.value?.bomId ? false : true
})

// 提交
const preservation = () => {
  const Obj = handleSelect.value
  Obj.bomType = query.bomType
  emit('preservation', Obj)
}

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = ''
  }
  query.bomType = props.bomType
  getList()
}

// 查询 Bom
const getList = async () => {
  query.bomType = props.bomType
  try {
    loading.value = true
    const res = await GetBomAPI(query)
    total.value = res.total
    TableData.value = res.rows
  } catch {
  } finally {
    loading.value = false
  }
}
getList()
</script>
<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :inline="true" label-width="100px">
      <el-form-item label="BOM编码">
        <el-input clearable v-model.trim="query.bomCode" placeholder="输入客户名称" />
      </el-form-item>
    </el-form>

    <el-divider />
    <el-table
      stripe
      border
      :loading="loading"
      :data="TableData"
      highlight-current-row
      @current-change="handleSelectionChange"
    >
      <el-table-column label="序号" width="80" type="index" />
      <el-table-column width="80">
        <template #default="scope">
          <el-radio-group v-model="handleSelect.bomId">
            <el-radio :label="scope.row.bomId || ''" size="large">&nbsp;</el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
      <el-table-column label="物料编码" prop="materialCode" width="auto" />
      <el-table-column label="物料名称" prop="materialName" width="auto" />
      <el-table-column label="物料规格" prop="materialSpec" width="auto" />
    </el-table>
    <div class="pagination">
      <pagination
        :fixed="false"
        v-show="total > 0"
        :total="total"
        v-model:page="query.pageNum"
        v-model:limit="query.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 按钮 -->
    <el-row class="Btn">
      <el-button type="primary" @click="getList">查询列表</el-button>
      <el-button type="info" @click="reset">重置</el-button>
      <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
      <el-button type="info" @click="emit('cancel')">取消</el-button>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
.app-container {
  width: 100%;
}
</style>
